import yaml
import sys

def read_yaml(file_path):
    with open(file_path, 'r') as f:
        return yaml.safe_load(f)

def write_yaml(data, file_path):
    with open(file_path, 'w') as f:
        yaml.dump(data, f)

if __name__ == '__main__':
    operation = sys.argv[1]
    in_file = sys.argv[2]
    
    if operation == 'read':
        data = read_yaml(in_file)
        # A simple way to pass data back to PowerShell is to convert to JSON
        import json
        print(json.dumps(data))
    elif operation == 'write':
        # This part is more complex as it requires passing complex data from PS to Python
        # For this script, we will do the modification in python directly.
        out_file = sys.argv[3]
        task_id_to_set = sys.argv[4]
        
        config_data = read_yaml(in_file)
        
        subset_dir = "d:/Alpha-SQL/temp_subsets"
        import os
        if not os.path.exists(subset_dir):
            os.makedirs(subset_dir)
        
        subset_file = os.path.join(subset_dir, f"subset_task_{task_id_to_set}.txt")
        with open(subset_file, 'w') as f:
            f.write(str(task_id_to_set))
            
        config_data['subset_file_path'] = subset_file
        config_data['n_processes'] = 1
        
        write_yaml(config_data, out_file)
